package com.tdam.g12;

import java.util.ArrayList;

import com.tdma.g12.provider.ConnectionStatusProvider;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListaHistorialConectividad extends ListActivity
{
	private ConectividadAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_historial_conectividad);
		adapter = new ConectividadAdapter();
		setListAdapter(adapter);
		loadData();
		adapter.notifyDataSetChanged();
	}
	public void loadData() {
		Cursor cur = this.getContentResolver().query(ConnectionStatusProvider.CONTENT_URI,null, String.valueOf(ConnectionStatusProvider.HISTORIAL), null, null);
		while(cur.moveToNext()){
			Registro aux = new Registro();
			aux.fecha = cur.getString(1);
			if( cur.getInt(2) == 1 )
				aux.estado = getString(R.string.histConectado);
			else
				aux.estado = getString(R.string.histDesconectado);
			adapter.addRegistro(aux);
		}
		cur.close();
	}
	class Registro
	{
		public String estado;
		public String fecha;
	}
	
	class ConectividadAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private ArrayList<Registro> data;
		public ConectividadAdapter() {
			inflater = LayoutInflater.from(ListaHistorialConectividad.this);
			data = new ArrayList<ListaHistorialConectividad.Registro>();
		}

		public void addRegistro(Registro reg) {
			if (reg != null) {
				data.add(reg);
			}
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		class Holder {
			private TextView txtEstado;
			private TextView txtFecha;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			Holder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_historial_conectividad, null);
				holder = new Holder();
				holder.txtEstado = (TextView) convertView.findViewById(R.id.txtHistConConexion);
				holder.txtFecha = (TextView) convertView.findViewById(R.id.txtHistConDate);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}

			Registro info = (Registro) getItem(position);
			holder.txtEstado.setText(info.estado);
			holder.txtFecha.setText(info.fecha);
			return convertView;
		}

	}
}
